<?php


namespace backend\flow_down;
use Yii;
use izyue\admin\models\Member;
use izyue\admin\models\MemberSearch;
use izyue\admin\models\DataOrder;
use izyue\admin\models\DataOrderSearch;
class DataToDownApi {

	/*
		流量平台针对下游用户接口
	 */
    public function api_submit() {
    	print_r(Yii::$app->request->post());exit;
    	if (empty($_POST['apikey']) || 
    		empty($_POST['sign']) || 
    		empty($_POST['phone_no']) || 
    		empty($_POST['flow_val']) || 
    		empty($_POST['product_code']) || 
    		empty($_POST['out_order_id']) || 
    		empty($_POST['timetamp']))	
    	{
    		$result = array(
    			'result' => -1, 
    			'err_desc' => '参数错误' 
    			);
    		return json_decode($result);
    	}
    	$sql = "select * from admin where apikey = " . $_POST['apikey'];
		$res = Member::findBySql($sql)->all();
		if (empty($res)) {
			$result = array(
				'result' => -1,
				'err_desc' => '参数错误' 
				);
    		return json_decode($result);
		} else {
			foreach ($res as $row) {
				if (MD5($row['apikey'].$row['username']) != $_POST['sign']) {
					$result = array(
						'result' => -3,
						'err_desc' => '鉴权错误'
						);
		    		return json_decode($result);
		    	}
				// } else {
				// 	$result = array('result' => 0,'order_id' => '', 'out_order_id'=> '', 'err_desc' => '');
		  		//  	return json_decode($result);
				// }
			}
		}		
	}
	public function api_blance() {
		if (empty($_POST['apikey']) || 
    		empty($_POST['sign'])	
    	{
    		$result = array(
    			'result' => -1,
    			'err_desc' => '参数错误'
    			);
    		return json_decode($result);
    	}
    	$sql = "select * from admin where apikey = " . $_POST['apikey'];
		$res = Member::findBySql($sql)->all();

		if (empty($res)) {
			$result = array(
				'result' => -4,
				'err_desc' => '账号异常'
				);
    		return json_decode($result);
		} else {
			foreach ($res as $row) {
				if (MD5($row['apikey'].$row['username']) != $_POST['sign']) {
					$result = array(
						'result' => -3,
						'err_desc' => '鉴权错误'
						);
		    		return json_decode($result);
				} else {
					$result = array(
						'result' => 0, 
						'blance' => $row['balance'], 
						'err_desc' => ''
						);
    				return json_decode($result);
				}			
			}		
		}
	}
	public function api_query() {
    	if (empty($_POST['apikey']) || 
    		empty($_POST['sign']) || 
    		empty($_POST['out_order_id'])	
    	{
    		$result = array(
    			'result' => -1,
    			'err_desc' => '参数错误'
    			);
    		return json_decode($result);
    	}
    	$sql = "select * from admin where apikey = " . $_POST['apikey'];
		$res = Member::findBySql($sql)->all();
		if (empty($res)) {
			$result = array(
				'result' => -4,
				'err_desc' => '账号异常'
				);
    		return json_decode($result);
		} else {
			foreach ($res as $row) {
				if (MD5($row['apikey'].$row['username']) != $_POST['sign']) {
					$result = array(
						'result' => -3,
						'err_desc' => '鉴权错误'
						);
		    		return json_decode($result);
				} 			
			}		
		}
    	$ordersql = "select * from data_order where orsersn = " . $_POST['out_order_id'];
		$orderres = DataOrder::findBySql($sql)->all();
		if (empty($orderres)) {
			$result = array(
				'result' => -1,
				'err_desc' => '参数错误'
				);
    		return json_decode($result);
		} else {
			foreach ($orderres as $row) {

				$result = array(
					'result' => 0, 
					'order_id' => '',  //上游接口内置订单id
					'out_order_id' => $row['ordersn'],
					'phone_no' => , //订单手机号码
					'status' => $row['status'], 
					'report_time' => '', //当前查询时间
					'err_desc' => ''
					);
				return json_decode($result);
				
			}
			
		}
	}
	public function api_back_url ($out_order_id) {
		if ($_POST['']) {

		}
	}
}
